<template>
	<div>
		<FormPlus label-width="120px" ref="baseForm" :formColumns="formColumns" :form-rules="formRules">
			<!-- 操作按钮 -->
			<template #Actions>
				<div style="text-align: center">
					<el-button type="primary" @click="onSubmit(baseForm)">提交</el-button>
					<el-button @click="handlerReset">重置</el-button>
				</div>
			</template>
		</FormPlus>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import FormPlus from '@/components/FormPlus/index.vue'
import { ElMessage, FormInstance } from 'element-plus'
import { getUserFromStore } from '@/common/useStore'
import { api } from '@/api'
const { RequestNoticeAdd } = api
const baseForm = ref(null)
const emits = defineEmits(['close'])
const formColumns = reactive([
	{
		xType: 'Input',
		label: '公告内容',
		prop: 'content',
		type: 'textarea',
		span: 16
	},
	{
		slotName: 'Actions',
		span: 16
	}
])
const formRules = ref({
	content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
})
const handlerReset = () => {
	baseForm.value?.formRef.resetFields()
}
// 提交操作
async function onSubmit(formEl: FormInstance | undefined) {
	if (!formEl) return
	await formEl.formRef.validate((valid: any, fields: any) => {
		if (!valid) return
		addNotice(baseForm.value?.formModel)
	})
}
/**
 * 添加公告
 */
const addNotice = async (formModel: any) => {
	const account = getUserFromStore()
	const data = await RequestNoticeAdd(formModel.content, account.mid)
	const { resultCode } = data
	if (resultCode === 1) {
		ElMessage({
			message: '添加公告成功!',
			type: 'success'
		})
        emits('close')
	}
}
</script>

<style scoped></style>
